package stream.model;

import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @package stream.model
 * @Class Test
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-10-8 上午10:24
 */
public class StreamTest {

    private List<StudentScore> studentScoreList = new ArrayList<>();
    @Test
    public void handlerDataJava8Stream() {
        //过滤85以上，并且按分数排名
        System.out.println("过滤85以上，并且按分数排名");
        List<StudentScore> collect = studentScoreList.stream().filter(item -> item.getScore() >= 85).sorted(new Comparator<StudentScore>() {
            @Override
            public int compare(StudentScore o1, StudentScore o2) {
                return o1.getScore()-o2.getScore();
            }
        }).collect(Collectors.toList());
        collect.forEach(item->System.out.println(item));
        //获取分数最高的那位学生姓名
        System.out.println("获取分数最高的那位学生姓名");
        Optional<String> s = studentScoreList.stream().max(Comparator.comparingInt(StudentScore::getScore)).map(StudentScore::getStuName);
        System.out.println(s.get());
        //按课程分组求课程平均分
        System.out.println("按课程分组求课程平均分");
        Map<String, Double> collect1 = studentScoreList.stream().collect(Collectors.groupingBy(StudentScore::getClassName, Collectors.averagingDouble(StudentScore::getScore)));
        collect1.forEach((key,value)->System.out.println(key+"="+value));
    }
    @Test
    public void getClassAvg() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        for (StudentScore studentScore : studentScoreList) {
            String className = studentScore.getClassName();
            if (map.containsKey(className)) {
                List<StudentScore> list = (List) map.get(className);
                list.add(studentScore);
            } else {
                List<StudentScore> list = new ArrayList<>();
                list.add(studentScore);
                map.put(className, list);
            }
        }
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            List<StudentScore> list = (List<StudentScore>) entry.getValue();
            Integer total = 0;
            for (StudentScore studentScore : list) {
                total += studentScore.getScore();
            }
            resultMap.put(entry.getKey(), total / list.size());
        }
        Set<Map.Entry<String, Object>> entries1 = resultMap.entrySet();
        System.out.println("分组后的数据");
        for (Map.Entry<String, Object> item : entries1) {
            System.out.println(item);
        }
    }
    @Test
    public void getMaxScoreTest() {
        String maxStuNname = studentScoreList.get(0).getStuName();
        Integer maxScore = studentScoreList.get(0).getScore();
        for (StudentScore studentScore : studentScoreList) {
            if (maxScore < studentScore.getScore()) {
                maxScore = studentScore.getScore();
                maxStuNname = studentScore.getStuName();
            }
        }
        System.out.println("最高分的学生名称: "+ maxStuNname);
    }
    @Test
    public void scoreTest() {
        Iterator<StudentScore> iterator = studentScoreList.iterator();
        while (iterator.hasNext()) {
            StudentScore studentScore = iterator.next();
            if (studentScore.getScore() < 85) {
                iterator.remove();
            }
        }
        Collections.sort(studentScoreList, new Comparator<StudentScore>() {
            @Override
            public int compare(StudentScore o1, StudentScore o2) {
                return o1.getScore()-o2.getScore();
            }
        });

    }
    public void print() {
        System.out.println("----------------------s--------------------------");
        studentScoreList.forEach(item->System.out.println(item));
        System.out.println("----------------------e--------------------------");
    }
    //@After
    public void handlerData() {
        print();
    }
    @Before
    public void initData() {
        StudentScore studentScore1 = new StudentScore() {{
            setStuName("张三");
            setClassName("语文");
            setScore(70);
        }};
        StudentScore studentScore2 = new StudentScore() {{
            setStuName("张三");
            setClassName("数学");
            setScore(80);
        }};
        StudentScore studentScore3 = new StudentScore() {{
            setStuName("张三");
            setClassName("英语");
            setScore(65);
        }};
        StudentScore studentScore4 = new StudentScore() {{
            setStuName("李四");
            setClassName("语文");
            setScore(68);
        }};
        StudentScore studentScore5 = new StudentScore() {{
            setStuName("李四");
            setClassName("数学");
            setScore(70);
        }};
        StudentScore studentScore6 = new StudentScore() {{
            setStuName("李四");
            setClassName("英语");
            setScore(90);
        }};
        StudentScore studentScore7 = new StudentScore() {{
            setStuName("王五");
            setClassName("语文");
            setScore(80);
        }};
        StudentScore studentScore8 = new StudentScore() {{
            setStuName("王五");
            setClassName("数学");
            setScore(85);
        }};
        StudentScore studentScore9 = new StudentScore() {{
            setStuName("王五");
            setClassName("英语");
            setScore(70);
        }};

        studentScoreList.add(studentScore1);
        studentScoreList.add(studentScore2);
        studentScoreList.add(studentScore3);
        studentScoreList.add(studentScore4);
        studentScoreList.add(studentScore5);
        studentScoreList.add(studentScore6);
        studentScoreList.add(studentScore7);
        studentScoreList.add(studentScore8);
        studentScoreList.add(studentScore9);
        print();
    }
}
